var Loading = {
    show: function () {
        if ($('.loading').length == 0) {
            var str = '<div class="loading"><img src="/assets/images/loading.gif"></div>';
            $('body').append(str);
        }

    },
    hide: function () {
        if ($('.loading').length > 0) {
            $('.loading').remove();
        }
    }
}

var util = {
    getCheck: function ($eles) {
        var arr = [];
        $eles.each(function (m, n) {
            if ($(n).prop('checked')) {
                arr.push($(n))
            }
        })
        return arr;
    }
}

//200字的输入框开始
$('.common-remark').on('input', function () {
    var length = $(this).val().trim().length
    if (length > 200) {
        alert('最多200字')
        $(this).val($(this).val().trim().substr(0, 200));
        return
    }
})
//200字的输入框结束

//录音弹窗关闭

$('.cms-modal-close').on('click', function () {
    $(this).parents('.cms-modal-lg').find('audio').get(0).pause();
    $(this).parents('.cms-modal-lg').hide();

})


/*
 *
 * 封装一个公共方法，用来加载loading和关闭loading，并且判断有没有数据，是不是需要提示暂无数据，提供用户体验度
 *
 * 六个个参数分别是，生成页面需要回调方法，点击页码回调的方法，用来判断的数据长度，搜索数据总条数，当前页码值，插入元素的ID
 * */

function v_data(callback, callback2, d_length, d_total, e_id) {


    var tmpl = '';

    if (d_length != 0) {

        tmpl = callback();

    } else {

        tmpl = '<tr><td colspan="13" style="padding: 3rem 0;"><h4>暂无数据！</h4></td></tr>';

    }

    $("#" + e_id).html(tmpl);

    $("#Pagination").pagination(d_total, {
        current_page: pageNum - 1,
        num_edge_entries: 2, //边缘页数
        num_display_entries: 8, //主体页数
        callback: function (page_index, jq) {

            if (pageNum != page_index + 1) {

                pageNum = page_index += 1;

                callback2();

            } else {
                pageNum = page_index += 1;
            }


        }, //回调函数
        items_per_page: 10, //每页显示1项
        prev_text: "上一页",
        next_text: "下一页",
        ellipse_text: "...",//省略的文本
        prev_show_always: true,//是否总显示“前一页”分页按钮
        next_show_always: true,//是否总显示“下一页”分页按钮，
    });

    Loading.hide();
}


/*
 *
 * 封装一个公共方法，用来加载loading和关闭loading，并且判断有没有数据，是不是需要提示暂无数据，提供用户体验度
 *
 * 六个个参数分别是，生成页面需要回调方法，点击页码回调的方法，用来判断的数据长度，搜索数据总条数，当前页码值，插入元素的ID
 * */

function v_data2(callback, callback2, d_length, d_total, e_id) {


    var tmpl = '';

    if (d_length != 0) {

        tmpl = callback();

    } else {

        tmpl = '<tr><td colspan="18" style="padding: 3rem 0;"><h4>暂无数据！</h4></td></tr>';

    }

    $("#" + e_id).html(tmpl);

    $("#Pagination1").pagination(d_total, {
        current_page: pageNum2 - 1,
        num_edge_entries: 2, //边缘页数
        num_display_entries: 8, //主体页数
        callback: function (page_index, jq) {

            if (pageNum2 != page_index + 1) {

                pageNum2 = page_index += 1;

                callback2();

            } else {
                pageNum2 = page_index += 1;
            }


        }, //回调函数
        items_per_page: 10, //每页显示1项
        prev_text: "上一页",
        next_text: "下一页",
        ellipse_text: "...",//省略的文本
        prev_show_always: true,//是否总显示“前一页”分页按钮
        next_show_always: true,//是否总显示“下一页”分页按钮，
    });

    Loading.hide();
}


/*
 *
 * 封装一个公共方法，用来加载loading和关闭loading，并且判断有没有数据，是不是需要提示暂无数据，提供用户体验度
 *
 * 六个个参数分别是，生成页面需要回调方法，点击页码回调的方法，用来判断的数据长度，搜索数据总条数，当前页码值，插入元素的ID
 * */

function v_data3(callback, callback2, d_length, d_total, e_id) {


    var tmpl = '';

    if (d_length != 0) {

        tmpl = callback();

    } else {

        tmpl = '<tr><td colspan="18" style="padding: 3rem 0;"><h4>暂无数据！</h4></td></tr>';

    }

    $("#" + e_id).html(tmpl);

    $("#Pagination2").pagination(d_total, {
        current_page: pageNum3 - 1,
        num_edge_entries: 2, //边缘页数
        num_display_entries: 8, //主体页数
        callback: function (page_index, jq) {

            if (pageNum3 != page_index + 1) {

                pageNum3 = page_index += 1;

                callback2();

            } else {
                pageNum3 = page_index += 1;
            }


        }, //回调函数
        items_per_page: 10, //每页显示1项
        prev_text: "上一页",
        next_text: "下一页",
        ellipse_text: "...",//省略的文本
        prev_show_always: true,//是否总显示“前一页”分页按钮
        next_show_always: true,//是否总显示“下一页”分页按钮，
    });

    Loading.hide();
}


/**
 * 封装插入HTML的代码
 * @param id
 * @param html
 */
function setHtml(id, html) {
    var ele = $("#" + id);
    if (ele.length > 0) {
        ele.html(html == null ? "" : html);
    }
}


/**
 *时间格式化
 */
function backLawTime(t) {


    if (t != undefined) {

        var str = new Date(t).Format("yyyy-MM-dd hh:mm:ss");
        return str;

    } else {

        return '';
    }


}


/**
 * 返回时间格式第二种
 * @param t
 * @returns {string}
 */
function backLawTime2(t) {

    if (t != undefined) {
        t += ""
        var pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
        var formatedDate = t.replace(pattern, '$1-$2-$3 $4:$5:$6');

        return formatedDate;

    } else {

        return '';
    }

}


/**
 *时间格式化
 */
function backState(t) {


    switch (t) {
        case 0:

            return '查看/编辑'
        case 1:
            return '查看';

        default :
            return '-';

    }


}


//权限判定用于隐藏按钮
if (role) {
    if (role == 1) {
        $("div[name='roleMenu']").hide();
    }

}


/**
 * 金额处理
 * 增加小数位
 **/
function back_money_handler(money) {
    if (money != null && money != undefined && money != "") {
        var reg = /.*\..*/; //是否包含小数点
       if( !reg.test(money)){
           return money+".00";
       }else{
           return money;
       }
    }else{
        return "0.00";
    }
}


/**
 *   返回各种状态
 * @param state
 * @returns {*}
 */

function backCallState(call_state, reject_state) {

    switch (call_state) {

        case 1:
            return (reject_state == 1 ? '主叫方' : '被叫方') + '挂断';
        case 2:
            return (reject_state == 1 ? '主叫方' : '被叫方') + '未接通';
        case 3:
            return (reject_state == 1 ? '主叫方' : '被叫方') + '拒接';
        case 4:
            return '拨打失败';
        case 5:
            return '异常状态';
    }
}
/**
 *   案源状态
 * @param aystat
 * @returns {*}
 */

function renderAystat(aystat) {

    switch (aystat) {
        case 0:
            return '未发布';
        case 1:
            return '已发布';
        case 2:
            return '已接单';
        case 3:
            return '已结案';
        case 4:
            return '已结算';
    }
}
/**
 *   客户付费意愿
 * @param uwant
 * @returns {*}
 */
function renderUwant(uwant) {

    switch (uwant) {
        case 0:
            return '无'
        case 1:
            return '一般';
        case 2:
            return '愿意';
        case 3:
            return '强烈';

    }
}
/**
 *   是否回款
 * @param uwant
 * @returns {*}
 */
function renderIsreturn(isreturn) {

    switch (isreturn) {

        case 0:
            return '未回款';
        case 1:
            return '接单回款';
        case 2:
            return '上报回款';

    }
}

function getFormatDate(date) {
    if (!date) {
        return '';
    }
    date = new Date(date);
    var seperator1 = "-";
    var seperator2 = ":";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    var second = date.getSeconds();
    var min=date.getMinutes();
    var hour=date.getHours();

    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    if (second >= 0 && second <= 9) {
        second = "0" + second;
    }
    if (min >= 0 && min <= 9) {
        min = "0" + min;
    }
    if (hour >= 0 && hour <= 9) {
        hour = "0" + hour;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate
        + " " + hour + seperator2 + min
        + seperator2 + second;
    return currentdate;
}
//发起时刻
$('#start-time').on('focus', function () {
    WdatePicker({
        //minDate:'%y-%M-{%d+1}' ,
        maxDate: '#F{$dp.$D(\'end-time\',{d:0});}',
        onpicked: function () {
            console.log($(this).val());
        }
    })
});
//发起时刻
$('#end-time').on('focus', function () {
    WdatePicker({
        minDate: '#F{$dp.$D(\'start-time\',{d:0});}',
        onpicked: function () {
            console.log($(this).val());
        }

    })
});

//账号创建开始时间
$('#create-start').on('focus',function(){
    WdatePicker({
        //minDate:'%y-%M-{%d+1}' ,
        maxDate:'#F{$dp.$D(\'create-end\',{d:0});}',
        onpicked:function(){
            console.log($(this).val());
        }
    })
})
//账号创建结束时间
$('#create-end').on('focus',function(){
    WdatePicker({
        minDate:'#F{$dp.$D(\'create-start\',{d:0});}',
        onpicked:function(){
            console.log($(this).val());
        }

    })
})
//首次咨询时间
$('#first-consult-start').on('focus',function(){
    WdatePicker({
        //minDate:'%y-%M-{%d+1}' ,
        maxDate:'#F{$dp.$D(\'first-consult-end\',{d:0});}',
        onpicked:function(){
            console.log($(this).val());
        }
    })
})
//首次咨询时间
$('#first-consult-end').on('focus',function(){
    WdatePicker({
        minDate:'#F{$dp.$D(\'first-consult-start\',{d:0});}',
        onpicked:function(){
            console.log($(this).val());
        }

    })
})
//末次咨询时间
$('#last-consult-start').on('focus',function(){
    WdatePicker({
        //minDate:'%y-%M-{%d+1}' ,
        maxDate:'#F{$dp.$D(\'last-consult-end\',{d:0});}',
        onpicked:function(){
            console.log($(this).val());
        }
    })
})
//末次咨询时间
$('#last-consult-end').on('focus',function(){
    WdatePicker({
        minDate:'#F{$dp.$D(\'last-consult-start\',{d:0});}',
        onpicked:function(){
            console.log($(this).val());
        }

    })
})

//咨询发起时刻
$('#consult-time-start').on('focus', function () {
    WdatePicker({
        //minDate:'%y-%M-{%d+1}' ,
        maxDate: '#F{$dp.$D(\'consult-time-end\',{d:0});}',
        onpicked: function () {

        }
    })
})
$('#consult-time-end').on('focus', function () {
    WdatePicker({
        minDate: '#F{$dp.$D(\'consult-time-start\',{d:0});}',
        onpicked: function () {

        }

    })
})
//提现时间
$('#withdraw-cash-start').on('focus',function(){
    WdatePicker({
        //minDate:'%y-%M-{%d+1}' ,
        maxDate:'#F{$dp.$D(\'withdraw-cash-end\',{d:0});}',
        onpicked:function(){
            console.log($(this).val());
        }
    })
})
$('#withdraw-cash-end').on('focus',function(){
    WdatePicker({
        minDate:'#F{$dp.$D(\'withdraw-cash-start\',{d:0});}',
        onpicked:function(){
            console.log($(this).val());
        }

    })
})